package com.saifei.match.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.saifei.common.R;
import com.saifei.match.entity.SaifeiMatch;
import com.saifei.match.entity.vo.MatchListVo;
import com.saifei.match.service.SaifeiMatchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Genius小凡
 * @since 2021-02-18
 */
@RestController
@RequestMapping("/match/match")
@CrossOrigin
public class SaifeiMatchController {

    /*
    带条件查询比赛
     */
    @Autowired
    SaifeiMatchService saifeiMatchService;

    @GetMapping("list")
    public R list() {
        List<SaifeiMatch> matchlist = saifeiMatchService.listAll();
        return R.ok().data("matchList", matchlist);
    }

    @PostMapping("listPage/{current}/{size}")
    public R listPage(@PathVariable("current") Long current, @PathVariable("size") Long size,  @RequestBody(required = false) MatchListVo vo) {
        Page<SaifeiMatch> page = new Page<>(current, size);
        QueryWrapper<SaifeiMatch> wrapper = new QueryWrapper<>();
        if(vo == null) {
            saifeiMatchService.page(page);
        }
        else {
            if(!StringUtils.isEmpty((vo.getMatchName()))) {
                wrapper.like("match_name", vo.getMatchName());
            }
            if(!StringUtils.isEmpty((vo.getMatchType()))) {
                wrapper.eq("match_type", vo.getMatchType());
            }
            if(!StringUtils.isEmpty((vo.getMatchStart()))) {
                wrapper.gt("match_start", vo.getMatchStart()); //大于
            }
            if(!StringUtils.isEmpty((vo.getMatchEnd()))) {
                wrapper.lt("match_end", vo.getMatchEnd()); //小于
            }
            if(!StringUtils.isEmpty((vo.getMatchLevel()))) {
                wrapper.eq("match_level", vo.getMatchLevel());
            }
            saifeiMatchService.page(page, wrapper);
        }
        List<SaifeiMatch> records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("records", records).data("total", total);
    }

}

